package com.base.helper;

import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Maps;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Log4j
public class SCZHHelper {
    private static final Integer companyid=48;

    private static final String appkey="28c8ad33e11b11ea840a00163e185f3b";

    private static final String callUrl="http://cc.antcallback.com/api/api_interface.php";

    private static final String appsecret="e1e1b1ac90bad";

    private static final String callAction="SendDial";

    private static final String addAction="AddUser";

    /**
     * 拨打电话接口
     * @param caller
     * @param callee
     */
    public static String callPhone(String caller,String callee,String uuid) throws Exception{
        Map<String,Object> maps= Maps.newHashMap();
        maps.put("action",callAction);
        maps.put("companyid",companyid);
        maps.put("appkey",appkey);
        maps.put("signature",getMd5Secret());
        maps.put("callernum",caller);
        maps.put("calleenum",callee);
        maps.put("userdata",uuid);
        String result= WebHelper.GetData(callUrl,getUrlParamsByMap(maps));
        log.warn("[震鸿电话拨号]"+result);
        JSONObject jsonObject=JSONObject.parseObject(result);

        if(jsonObject.getString("errno").equals("14")){
            String registerResult=registerMobile(caller);
            JSONObject jsonObject1=JSONObject.parseObject(registerResult);
            if (jsonObject1.getString("errno").equals("1")){
                throw new Exception("该用户无法使用13号线路");
            }
            result= callPhone(caller,callee,uuid);
        }
        return result;
    }

    public static String registerMobile(String caller){
        Map<String,Object> maps= Maps.newHashMap();
        maps.put("action",addAction);
        maps.put("companyid",companyid);
        maps.put("appkey",appkey);
        maps.put("signature",getMd5Secret());
        maps.put("phone",caller);
        maps.put("password","123456");
        String result= WebHelper.GetData(callUrl,getUrlParamsByMap(maps));
        log.warn("[震鸿电话注册]"+result);
        return result;
    }

    private static String getMd5Secret(){
        return getMD5Str(appkey+appsecret);
    }

    public static String getUrlParamsByMap(Map<String, Object> map) {
        if (map == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = StringUtils.substringBeforeLast(s, "&");
        }
        return s;
    }

    public static String getMD5Str(String str) {
        byte[] digest = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest  = md5.digest(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //16是表示转换为16进制数
        String md5Str = new BigInteger(1, digest).toString(16);
        return md5Str;
    }

    public static void main(String[] args) {
        String result= null;
        try {
            result = callPhone("17674612951","17344451035","pqtestno2");
        } catch (Exception e) {
            System.out.println("该用户无法使用13号线路");
            e.printStackTrace();
        }
        System.out.println(result);
    }



}
